<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button
      type="primary"
      plain
      @click="openForm('create')"
      v-hasPermi="['erp:pur-raw-material-quotation:create']"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="材质编号" align="center" prop="id" />
      <el-table-column label="材质名称" align="center" prop="materialName" />
      <el-table-column label="材质类型" align="center" prop="materialType" />
      <el-table-column label="材质类型id" align="center" prop="materialTypeId" />
      <el-table-column label="楞别" align="center" prop="fluteType" />
      <el-table-column label="层数" align="center" prop="layer" />
      <el-table-column label="密度" align="center" prop="density" />
      <el-table-column label="厚度" align="center" prop="thickness" />
      <el-table-column label="耐破" align="center" prop="burstStrength" />
      <el-table-column label="边压" align="center" prop="edgeCompression" />
      <el-table-column label="面纸" align="center" prop="facePaper" />
      <el-table-column label="瓦楞纸1" align="center" prop="flutePaper1" />
      <el-table-column label="芯纸1" align="center" prop="corePaper1" />
      <el-table-column label="瓦楞纸2" align="center" prop="flutePaper2" />
      <el-table-column label="芯纸2" align="center" prop="corePaper2" />
      <el-table-column label="瓦楞纸3" align="center" prop="flutePaper3" />
      <el-table-column label="里纸" align="center" prop="innerPaper" />
      <el-table-column label="供应商备注" align="center" prop="supplierRemark" />
      <el-table-column label="成本单价" align="center" prop="costPrice" />
      <el-table-column label="密度单位" align="center" prop="densityUnit" />
      <el-table-column label="是否库存预警" align="center" prop="stockWarning" />
      <el-table-column label="库存下限" align="center" prop="stockLowerLimit" />
      <el-table-column label="库存上限" align="center" prop="stockUpperLimit" />
      <el-table-column label="面纸名称" align="center" prop="facePaperName" />
      <el-table-column label="瓦楞纸1名称" align="center" prop="flutePaper1Name" />
      <el-table-column label="瓦楞纸2名称" align="center" prop="flutePaper2Name" />
      <el-table-column label="瓦楞纸3名称" align="center" prop="flutePaper3Name" />
      <el-table-column label="里纸名称" align="center" prop="innerPaperName" />
      <el-table-column label="瓦楞纸1坑型" align="center" prop="flutePaper1FluteType" />
      <el-table-column label="瓦楞纸2坑型" align="center" prop="flutePaper2FluteType" />
      <el-table-column label="瓦楞纸3坑型" align="center" prop="flutePaper3FluteType" />
      <el-table-column label="生管对应名称" align="center" prop="productionCorrespondingName" />
      <el-table-column label="配置" align="center" prop="configuration" />
      <el-table-column label="是否为基础材质" align="center" prop="isBaseMaterial" />
      <el-table-column label="科目" align="center" prop="subject" />
      <el-table-column label="是否停用" align="center" prop="isDisabled" />
      <el-table-column label="生管材质对照2" align="center" prop="productionMaterialCorrespondence2" />
      <el-table-column label="生管材质对照3" align="center" prop="productionMaterialCorrespondence3" />
      <el-table-column label="生管材质对照4" align="center" prop="productionMaterialCorrespondence4" />
      <el-table-column label="生管材质对照5" align="center" prop="productionMaterialCorrespondence5" />
      <el-table-column label="生管材质对照6" align="center" prop="productionMaterialCorrespondence6" />
      <el-table-column label="生管材质对照7" align="center" prop="productionMaterialCorrespondence7" />
      <el-table-column label="面纸克重" align="center" prop="facePaperWeight" />
      <el-table-column label="瓦楞纸1克重" align="center" prop="flutePaper1Weight" />
      <el-table-column label="芯纸1克重" align="center" prop="corePaper1Weight" />
      <el-table-column label="瓦楞纸2克重" align="center" prop="flutePaper2Weight" />
      <el-table-column label="芯纸2克重" align="center" prop="corePaper2Weight" />
      <el-table-column label="瓦楞纸3克重" align="center" prop="flutePaper3Weight" />
      <el-table-column label="里纸克重" align="center" prop="innerPaperWeight" />
      <el-table-column label="默认库存单位" align="center" prop="defaultStorageUnit" />
      <el-table-column label="代用材质" align="center" prop="substituteMaterial" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="物料id" align="center" prop="materialId" />
       <el-table-column label="基础价" align="center" prop="basePrice" />
      <el-table-column label="旧价" align="center" prop="oldPrice" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="起订数" align="center" prop="minimumOrderQuantity" />
      <el-table-column label="数量优惠" align="center" prop="quantityDiscount" />
      <el-table-column label="供应商材质名称" align="center" prop="supplierMaterialName" />
      <el-table-column label="基础材质" align="center" prop="baseMaterial" />
      <el-table-column label="加价" align="center" prop="markup" />
      <el-table-column label="最小厚度" align="center" prop="minThickness" />
      <el-table-column label="最大厚度" align="center" prop="maxThickness" />
      <el-table-column label="长" align="center" prop="length" />
      <el-table-column label="宽" align="center" prop="width" />
      <el-table-column label="报价单位" align="center" prop="quotationUnit" />
      <el-table-column label="不按原纸报价计算单价" align="center" prop="notBasedOnBasePaperUnitPrice" />
      <el-table-column label="启用" align="center" prop="enable" />
      <el-table-column label="特价" align="center" prop="specialPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="最小克重" align="center" prop="minDensity" />
      <el-table-column label="最大克重" align="center" prop="maxDensity" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:pur-raw-material-quotation:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:pur-raw-material-quotation:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
    <!-- 表单弹窗：添加/修改 -->
    <PurRawMaterialQuotationDetailForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { PurRawMaterialQuotationApi } from '@/api/erp/purrawmaterialquotation'
import PurRawMaterialQuotationDetailForm from './PurRawMaterialQuotationDetailForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  quotationId?: number // 报价单id（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  quotationId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.quotationId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.quotationId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PurRawMaterialQuotationApi.getPurRawMaterialQuotationDetailPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.quotationId) {
    message.error('请选择一个ERP采购管理-供应商原料报价主')
    return
  }
  formRef.value.open(type, id, props.quotationId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PurRawMaterialQuotationApi.deletePurRawMaterialQuotationDetail(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>